<?php

//防止恶意调用
if (!defined('IN_MOD'))
{
    die('Hacking attempt');
}

use app\portal\model\PortalCategoryModel;

$type = moduleaqvarval("type","string");

$user              = cmf_get_current_user();
$this->assign($user);

// 新增自定义模块独立表配置 DATE:2021-8-8 START
$moduletable = getmoduletable($configinfo['moduleid']);
// 新增自定义模块独立表配置 DATE:2021-8-8 END

// 加入前模块不同模块的配置
if($suppliermodule)
{
	include(MODULEPARA_PATH.'/configmodule/configmodule/module/'.$suppliermodule.'/supplier_before.php');
}

// 数据列表
if($type == 'list')
{
	$configinfo['smartyname'] = "supplier/list";// 设置切换模板
	
	$keyword = $_MGPC_REQ['keyword'];
	$this->assign('keyword',    $keyword);
}

// 数据添加
if($type == 'add')
{
	$configinfo['smartyname'] = "supplier/list_add";// 设置切换模板
	
	// 栏目分类
	$portalCategoryModel = new PortalCategoryModel();
	$categoriesTree      = $portalCategoryModel->adminCategoryTree($configinfo['columninfo']['id'],0,$configinfo['moduleid']);
	$this->assign('categories_tree', $categoriesTree);
	
	// 模块参数
	$contenthtml = configmoduleparameter();
	$this->assign('contenthtml',    $contenthtml);
}

// 数据插入
if ($type == 'addpost')
{
	$category_id = moduleaqvarval('category_id', 'intval');
	
	// 接收数据	
	$moduleaqvar = array();
	$moduleaqvar[0] = 'senddata-module';// 模块ID
	$moduleaqvar[1] = 'senddata-class2';// 二级栏目
	$moduleaqvar[2] = 'senddata-class3';// 三级栏目
	$moduleaqvar[3] = 'senddata-create_time';
	$moduleaqvar[4] = 'senddata-published_time';
	$moduleaqvar[5] = 'senddata-category_id';
	$moduleaqvar[6] = 'senddata-lang';
	$moduleaqvar[7] = 'senddata-user_id';
	$moduleaqvar[8] = 'senddata-user_name';
	$moduleaqcontent = array();
	$moduleaqcontent[0] = $configinfo['moduleid'];// 模块ID
	$moduleaqcontent[1] = intval($configinfo['moduleclass']['class2']['id']);// 二级栏目
	$moduleaqcontent[2] = intval($configinfo['moduleclass']['class3']['id']);// 三级栏目
	$moduleaqcontent[3] = time();
	$moduleaqcontent[4] = time();
	$moduleaqcontent[5] = $category_id;
	$moduleaqcontent[6] = $lang;
	$moduleaqcontent[7] = $configmember['id'];
	$moduleaqcontent[8] = $configmember['user_login'];
	moduleaqvardata($moduleaqvar,$moduleaqcontent);
	
	$senddata = moduleaqvarval('senddata','array');
		
	if (!empty($_MGPC_REQ['photo_names']) && !empty($_MGPC_REQ['photo_urls'])) {
		$senddata['more']['photos'] = [];
		foreach ($_MGPC_REQ['photo_urls'] as $key => $url) {
			$photoUrl = cmf_asset_relative_url($url);
			array_push($senddata['more']['photos'], ["url" => $photoUrl, "name" => $_MGPC_REQ['photo_names'][$key]]);
		}
	}
	
	if (!empty($_MGPC_REQ['file_names']) && !empty($_MGPC_REQ['file_urls'])) {
		$senddata['more']['files'] = [];
		foreach ($_MGPC_REQ['file_urls'] as $key => $url) {
			$fileUrl = cmf_asset_relative_url($url);
			array_push($senddata['more']['files'], ["url" => $fileUrl, "name" => $_MGPC_REQ['file_names'][$key]]);
		}
	}
	
	if (!empty($senddata['more']['thumbnail'])) {
		$senddata['more']['thumbnail'] = cmf_asset_relative_url($senddata['more']['thumbnail']);
		$senddata['thumbnail']         = $senddata['more']['thumbnail'];
	}

	if (!empty($senddata['more']['audio'])) {
		$senddata['more']['audio'] = cmf_asset_relative_url($senddata['more']['audio']);
	}

	if (!empty($senddata['more']['video'])) {
		$senddata['more']['video'] = cmf_asset_relative_url($senddata['more']['video']);
	}
	
	$senddata['more'] = json_encode($senddata['more']);
	
	// 插入数据
	$insert_id = insertdata($moduletable, $senddata);
	updatedata($moduletable,"`update_time` = '".time()."'",$configinfo['article_id'],$insert_id);
	
	// 开启TAG标签
	if($configbase['tagok'] == 1)
	{
		if(isset($senddata['post_keywords']))
		{
			$senddata['post_keywords'] = str_replace('，', ',', $senddata['post_keywords']);
	
			$keywords = explode(',', $senddata['post_keywords']);
			
			$PortalPostModel = new \app\portal\model\PortalPostModel();
			$PortalPostModel->addTags($keywords, $insert_id);
		}
	}
	
	$resdata = insertmoduleparameter($insert_id);
	
	// 错误提示
	if($resdata['error'] > 0)
	{
		$modulehook = "errortip";$tipcontent = $resdata['message'];require(MODULEPARA_PATH . 'config/modulehook.php');
	}
}

// 数据编辑
if($type == 'edit')
{
	$configinfo['smartyname'] = "supplier/list_edit";// 设置切换模板
	$actid = moduleaqvarval("actid", "intval");
	
	// 当前信息
	$modulewhere=array();
	$modulewhere[0]=$moduletable;
	$modulewhere[1]=$actid;
	$modulewhere[2]="id";
	$data=modulepara("datainfo",$modulewhere);	
	$data['more'] = json_decode($data['more'], true);
	$data['post_content'] = stzhtml($data['post_content']);
	$this->assign('data',    $data);
	
	// 栏目分类
	$portalCategoryModel = new PortalCategoryModel();
	$categoriesTree      = $portalCategoryModel->adminCategoryTree($data['category_id'],0,$configinfo['moduleid']);
	$this->assign('categories_tree', $categoriesTree);
	
	// 模块参数
	$contenthtml = configmoduleparameter($actid);
	$this->assign('contenthtml',    $contenthtml);
}

// 数据修改
if ($type == 'editpost')
{
	$actid = moduleaqvarval("actid", "intval");
	$category_id = moduleaqvarval('category_id', 'intval');
	
	// 接收数据	
	$moduleaqvar = array();
	$moduleaqvar[0] = 'senddata-category_id';
	$moduleaqcontent = array();
	$moduleaqcontent[0] = $category_id;
	moduleaqvardata($moduleaqvar,$moduleaqcontent);
	
	$senddata = moduleaqvarval('senddata','array');
		
	if (!empty($_MGPC_REQ['photo_names']) && !empty($_MGPC_REQ['photo_urls'])) {
		$senddata['more']['photos'] = [];
		foreach ($_MGPC_REQ['photo_urls'] as $key => $url) {
			$photoUrl = cmf_asset_relative_url($url);
			array_push($senddata['more']['photos'], ["url" => $photoUrl, "name" => $_MGPC_REQ['photo_names'][$key]]);
		}
	}
	
	if (!empty($_MGPC_REQ['file_names']) && !empty($_MGPC_REQ['file_urls'])) {
		$senddata['more']['files'] = [];
		foreach ($_MGPC_REQ['file_urls'] as $key => $url) {
			$fileUrl = cmf_asset_relative_url($url);
			array_push($senddata['more']['files'], ["url" => $fileUrl, "name" => $_MGPC_REQ['file_names'][$key]]);
		}
	}
	
	if (!empty($senddata['more']['thumbnail'])) {
		$senddata['more']['thumbnail'] = cmf_asset_relative_url($senddata['more']['thumbnail']);
		$senddata['thumbnail']         = $senddata['more']['thumbnail'];
	}

	if (!empty($senddata['more']['audio'])) {
		$senddata['more']['audio'] = cmf_asset_relative_url($senddata['more']['audio']);
	}

	if (!empty($senddata['more']['video'])) {
		$senddata['more']['video'] = cmf_asset_relative_url($senddata['more']['video']);
	}
	
	$senddata['more'] = json_encode($senddata['more']);

	// 修改数据
	updatedata($moduletable,$senddata,$configinfo['article_id'],$actid);
	updatedata($moduletable,"`update_time` = '".time()."'",$configinfo['article_id'],$actid);
	
	// 开启TAG标签
	if($configbase['tagok'] == 1)
	{
		if(isset($senddata['post_keywords']))
		{
			$senddata['post_keywords'] = str_replace('，', ',', $senddata['post_keywords']);
	
			$keywords = explode(',', $senddata['post_keywords']);
			
			$PortalPostModel = new \app\portal\model\PortalPostModel();
			$PortalPostModel->addTags($keywords, $actid);
		}
	}
	
	$resdata = updatemoduleparameter($actid);
	
	// 错误提示
	if($resdata['error'] > 0)
	{
		$modulehook = "errortip";$tipcontent = $resdata['message'];require(MODULEPARA_PATH . 'config/modulehook.php');
	}
}

// 数据删除
if ($type == 'del')
{	
	$actid = moduleaqvarval("actid", "intval");
	
	// 删除数据
	$deleteok = deletedata($moduletable, "id", $actid);
	if($deleteok == 0)$modulehook = "errortip";$tipcontent = "不可操作其它模块下的数据或当前模块操作数据不存在，非法操作！";require(MODULEPARA_PATH . 'config/modulehook.php');

	// 删除相关参数
	deletemoduleparameter($actid);
}

// 加入后模块不同模块的配置
if($suppliermodule)
{
	include(MODULEPARA_PATH.'/configmodule/configmodule/module/'.$suppliermodule.'/supplier_rear.php');
}

// 操作成功提示
include_once(MODULEPARA_PATH.'/configmodule/configmodule/module/'.$modulenamedir.'/'.$modulenamefile.'_tip.php');

?>